<template>
    <div class="index">
        <div class="top">
            <i class="menu-btn iconfont icon-menu" @click="menu=!menu"></i> <b>视频点播</b>
            <div class="user-name" @click="userNameFun">{{username}}</div>
        </div>
        <div class="menu" :style="menuStyle">
            <div class="item" @click="$router.push('/index')"><i class="iconfont icon-shouye"></i> 首页</div>
            <div  @click="$router.push('/index/like')" class="item"><i class="iconfont icon-xihuan"></i> 我的喜欢</div>
            <div v-if="role==='admin'"  @click="$router.push('/index/newvideo')" class="item"><i class="iconfont icon-xinjian"></i> 上传视频</div>
            <div v-if="uid" class="item" @click="$router.push('/index/user')"><i class="iconfont icon-user"></i> 个人中心</div>
            <div v-if="uid" class="item" @click="logout"><i class="iconfont icon-tuichudenglu"></i>退出登录</div>
        </div>
        <div class="content" :style="contentStyle">
            <router-view></router-view>
        </div>
    </div>

</template>
<script>
    import {Loading} from 'element-ui'

    export default {
        data() {
            return {
                menu: false,
                username: '登陆',
                uid: sessionStorage.uid,
                role:sessionStorage.role
            }
        },
        computed: {
            menuStyle() {
                let style = {}
                if (this.menu) {
                    style.left = '-240px'
                }
                return style

            },
            contentStyle() {
                let style = {}
                if (this.menu) {
                    style.width = '100%'
                    style.marginLeft = '0'
                }
                return style
            }
        },

        methods: {
            logout() {
                sessionStorage.clear()
                document.cookie = 'PHPSESSID=null'
                location.href = location.origin + location.pathname
            },
            /*点击登陆，用户名跳转相应页面*/
            userNameFun() {
                if (this.username === '登陆') {
                    this.$router.push('/login');
                } else {
                    this.$router.push('/index/user')
                }
            },
            /*判断用户是否登陆*/
            judgeLogin() {
                let uid = sessionStorage.getItem('uid');
                if (uid) {
                    this.username = sessionStorage.petname !== null ? sessionStorage.petname : sessionStorage.name;
                } else {
                    this.username = '登陆';
                }
            },
        },
        created: function () {
            this.judgeLogin();
        }
    }
</script>
<style scoped>
    .menu-btn {
        display: inline-block;
        color: #aaa;
        height: 45px;
        width: 45px;
        text-align: center;
        line-height: 45px;
        margin-left: 5px;
        font-size: 22px;
        border-radius: 40px;
    }

    .user-name {
        float: right;
        margin-right: 35px;
        font-size: 14px;
        cursor: pointer;
        color: red;
    }

    .menu-btn:active {
        background: #ddd;
    }

    .top {
        z-index: 10;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 56px;
        line-height: 56px;
        font-size: 20px;
        background: #ffffff;
    }

    .menu {
        position: fixed;
        top: 56px;
        left: 0;
        width: 240px;
        font-size: 14px;
        height: calc(100% - 56px);
        background: #e5e5e5;
    }

    .menu .item {
        text-align: left;
        line-height: 40px;
        width: 100%;
        height: 40px;
    }

    .iconfont {
        margin-left: 30px;
        margin-right: 12px;
        font-size: 20px;
    }

    .menu .item:hover {
        background: #d8d8d8;
    }

    .content {
        margin-left: 240px;
        margin-top: 56px;
        width: calc(100% - 240px);
    }


</style>